package com.gdyx.app.web.svc.impl;


import com.gdyx.app.web.dao.MpsDeptDao;
import com.gdyx.app.web.dao.MpsDeptRoleDao;
import com.gdyx.app.web.dao.MpsDeptUserDao;
import com.gdyx.app.web.dao.MpsOrgDeptDao;
import com.gdyx.app.web.model.MpsDept;
import com.gdyx.app.web.model.MpsDeptRole;
import com.gdyx.app.web.svc.MpsDeptSvc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by mac on 2017/7/27.
 */
@Service
public class MpsDeptSvcImpl implements MpsDeptSvc {
    @Autowired
    MpsDeptDao dao;
    @Autowired
    MpsDeptRoleDao mpsDeptRoleDao;
    @Autowired
    MpsDeptUserDao mpsDeptUserDao;
    @Autowired
    MpsOrgDeptDao mpsOrgDeptDao;


    @Transactional
    @Override
    public void save(MpsDept o, String uid) {
        //部门表增加
        o.setId(UUID.randomUUID().toString().toUpperCase().trim());
        o.setCreateOwner(uid);
        o.setCreateTime(new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()));
        //当前用户的部门
        dao.save(o);
        if (o.getRoles() != null) {
            //部门角色关联表增加
            for (String rid : o.getRoles()) {
                MpsDeptRole mpsDeptRole = new MpsDeptRole();
                mpsDeptRole.setId(UUID.randomUUID().toString().toUpperCase().trim());
                mpsDeptRole.setDeptId(o.getId());
                mpsDeptRole.setRoleId(rid);
                mpsDeptRole.setCreateOwner(uid);
                mpsDeptRole.setCreateTime(new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()));
                mpsDeptRoleDao.save(mpsDeptRole);
            }
        }
    }

    @Override
    public void saveOrUpdate(MpsDept o) {
        dao.saveOrUpdate(o);
    }

    @Transactional
    @Override
    public void delete(String id) {
        //删除部门角色关联表
        mpsDeptRoleDao.deleteByDid(id);
        //删除部门用户关联表
        mpsDeptUserDao.deleteByDid(id);
        dao.delete(id);
    }

    @Transactional
    @Override
    public void update(MpsDept o, String uid) {
        //删除对应部门角色关联表
        mpsDeptRoleDao.deleteByDid(o.getId());
        if (o.getRoles() != null) {
            //更新关联表
            for (String rid : o.getRoles()) {
                MpsDeptRole mpsDeptRole = new MpsDeptRole();
                mpsDeptRole.setId(UUID.randomUUID().toString().toUpperCase().trim());
                mpsDeptRole.setDeptId(o.getId());
                mpsDeptRole.setRoleId(rid);
                mpsDeptRole.setModifyOwner(uid);
                mpsDeptRole.setModifyTime(new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()));
                mpsDeptRoleDao.save(mpsDeptRole);
            }
        }
        //更新部门表
        o.setModifyOwner(uid);
        o.setModifyTime(new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()));
        dao.update(o);
    }

    @Override
    public MpsDept findMpsDeptById(String id) {
        return dao.findMpsDeptById(id);
    }

    @Override
    public int count(Map<String, Object> m) {
        return dao.count(m);
    }

    @Override
    public List<MpsDept> findMpsDepts(Map<String, Object> m) {
        return dao.findMpsDepts(m);
    }

    @Override
    public List<MpsDept> findMpsDeptByOId(String id) {
        return dao.findMpsDeptByOId(id);
    }

}
